import jsonpath


class GetKeyWord(object):
    @staticmethod
    def get_keyword(data, name):
        try:
            # 获取一个data里面的name(第一个出现的name)
            result = jsonpath.jsonpath(data, f'$..{name}')[0]
        except Exception as e:
            print(f"{name}获取失败,原因是{e}")
            return False
        else:
            return result

    @staticmethod
    def get_keywords(data, name):
        try:
            # 获取data里面的全部name
            result = jsonpath.jsonpath(data, f'$..{name}')
        except Exception as e:
            print(f"{name}获取失败,原因是{e}")
            return False
        else:
            return result


if __name__ == '__main__':
    data = {list: [{
        "name": "王二"},
        {"name": "张三"}
    ]}
    print(GetKeyWord.get_keyword(data, "name"))
    print(GetKeyWord.get_keywords(data, "name"))
